﻿using System;
using System.IO;
using System.Text;
using YArchitech.LIB;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace SystemDiagramNew
{
	public sealed class SystemDiagramOption
	{
		private SystemDiagramOption()
		{
			this.SelectionMothod = SelectionMothod.BySystem;
			this.SelectionRange = SelectionRange.All;
			this.IsVertical = false;
			this.Angle = 45.0;
			this.ScaleX = 1.0;
			this.ScaleY = 1.0;
			this.ScaleZ = 1.0;
			this.ScaleLegend = 1.0;
			this.LabelPipeDiameter = true;
			this.LabelElevation = true;
			this.Scale = 50;
			this.VerticalPipeLabelOffeset = 1000.0;
		}

		[JsonConverter(typeof(StringEnumConverter))]
		public SelectionMothod SelectionMothod { get; set; }

		[JsonConverter(typeof(StringEnumConverter))]
		public SelectionRange SelectionRange { get; set; }

		public bool IsVertical { get; set; }

		public double Angle { get; set; }

		public double ScaleX { get; set; }

		public double ScaleY { get; set; }

		public double ScaleZ { get; set; }

		public double ScaleLegend { get; set; }

		public bool LabelPipeDiameter { get; set; }

		public bool LabelElevation { get; set; }

		public int Scale { get; set; }

		public double VerticalPipeLabelOffeset { get; set; }

		public static SystemDiagramOption Instance
		{
			get
			{
				return SystemDiagramOption.LazyInstance.Value;
			}
		}

		private static SystemDiagramOption Load()
		{
			SystemDiagramOption systemDiagramOption = new SystemDiagramOption();
			if (!File.Exists(SystemDiagramOption.DataFileName))
			{
				return systemDiagramOption;
			}
			SystemDiagramOption result;
			try
			{
				result = (JsonConvert.DeserializeObject<SystemDiagramOption>(File.ReadAllText(SystemDiagramOption.DataFileName)) ?? systemDiagramOption);
			}
			catch
			{
				result = systemDiagramOption;
			}
			return result;
		}

		public void Save()
		{
			try
			{
				JsonConvert.SerializeObject(this).SaveToFile(SystemDiagramOption.DataFileName);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private static readonly Lazy<SystemDiagramOption> LazyInstance = new Lazy<SystemDiagramOption>(new Func<SystemDiagramOption>(SystemDiagramOption.Load));

		private static readonly string DataFileName = Path.Combine(Product.WorkingLocation, "SystemDiagram.json");
	}
}
